#coding:utf-8

man = []
other =[]
try:
    data = open("sketch.txt","r")
    for each_line in data:  #说实话这里有点不懂，为啥for循环出来就是一行行的呢
        try:
            role,line_spoken = each_line.split(":",1)
            line_spoken =line_spoken.strip()
            if role == "Man":
                man.append(line_spoken) #man说的话都在里面
            elif role == "Other Man":
                other.append(line_spoken)#other说的话都在里木
        except ValueError:
            pass
    data.close()
except IOError:
    print("The file data is missing!")

""


"""
try:
    man_file = open("man_data.txt","w")
    other_file = open("other_data.txt","w")
    print(man,file=man_file)  #用print写入
    print(other,file = other_file)  #用print写入数据
    #man_file.write(str(man))
    #other_file.write(str(other))  #用write写数据

except IOError:
    print("File Error")
finally:
    man_file.close()
    other_file.close()

"""
#用with open as重写上面的代码如下:
"""
try:
    with open("man_data.txt","w") as man_file:
        # man_file.write(str(man))
        print(man, file=man_file)
    with open("man_data.txt","w") as other_file:
        # other_file.write(str(other))
        print(other,file=other_file)
except IOError:
    print("File error！")
"""
#再或者把with写成一行：
try:
    with open("man_data.txt","w") as man_file,open("man_data.txt","w") as other_file:
        # man_file.write(str(man))
        print(man, file=man_file)
        # other_file.write(str(other))
        print(other,file=other_file)
except IOError:
    print("File error！")
